package eclipse_sync_plugin;

import org.eclipse.core.runtime.Status;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

public class Activator extends AbstractUIPlugin {
    // 插件ID
    public static final String PLUGIN_ID = "eclipse-sync-plugin";
    
    // 单例实例
    private static Activator plugin;
    
    // 插件是否已启动
    private boolean isStarted = false;
    
    public Activator() {
        try {
            // 在构造函数中记录日志
            if (plugin == null) {
                plugin = this;
                log(Status.INFO, "插件初始化开始");
            }
        } catch (Exception e) {
            // 由于此时插件可能还未完全初始化，使用System.out作为备选
            System.out.println("插件初始化失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;
        initializeDefaultPreferences(); // 在插件启动时初始化默认偏好设置
        log(Status.INFO, "Eclipse-Cursor同步插件已启动", null);
    }
    
    @Override
    public void stop(BundleContext context) throws Exception {
        if (isStarted) {
            stopPlugin();
        }
        plugin = null;
        super.stop(context);
        log(Status.INFO, "Eclipse-Cursor同步插件已停止", null);
    }
    
    public void startPlugin() throws Exception {
        if (!isStarted) {
            try {
                isStarted = true;
                log(Status.INFO, "插件已启动");
            } catch (Exception e) {
                log(Status.ERROR, "启动插件失败", e);
                // 添加System.out作为备选日志
                System.out.println("启动插件失败: " + e.getMessage());
                e.printStackTrace();
                throw e;
            }
        }
    }
    
    public void stopPlugin() throws Exception {
        if (isStarted) {
            try {
                isStarted = false;
                log(Status.INFO, "插件已停止");
            } catch (Exception e) {
                log(Status.ERROR, "停止插件失败", e);
                // 添加System.out作为备选日志
                System.out.println("停止插件失败: " + e.getMessage());
                e.printStackTrace();
                throw e;
            }
        }
    }
    
    public static Activator getDefault() {
        return plugin;
    }
    
    public void log(int severity, String message) {
        log(severity, message, null);
    }
    
    public void log(int severity, String message, Throwable exception) {
        try {
            getLog().log(new Status(severity, PLUGIN_ID, message, exception));
            // 添加System.out作为备选日志
            System.out.println("[" + getSeverityString(severity) + "] " + message);
            if (exception != null) {
                exception.printStackTrace();
            }
        } catch (Exception e) {
            // 如果Eclipse日志系统不可用，至少打印到控制台
            System.out.println("日志记录失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    private String getSeverityString(int severity) {
        switch (severity) {
            case Status.ERROR:
                return "错误";
            case Status.WARNING:
                return "警告";
            case Status.INFO:
                return "信息";
            default:
                return "未知";
        }
    }
    
    /**
     * 获取插件的偏好设置存储
     */
    public IPreferenceStore getPreferenceStore() {
        return super.getPreferenceStore();
    }
    
    /**
     * 初始化偏好设置的默认值
     */
    private void initializeDefaultPreferences() {
        IPreferenceStore store = getPreferenceStore();
        store.setDefault(PluginPreferencePage.PREF_ECLIPSE_PATH, "D:\\eclipse2024\\jee-2024-12\\eclipse\\eclipse.exe");
        store.setDefault(PluginPreferencePage.PREF_CURSOR_PATH, "C:\\Users\\ThinkPad\\AppData\\Local\\Programs\\cursor\\Cursor.exe");
        store.setDefault(PluginPreferencePage.PREF_PYTHON_PATH, "python");
        log(Status.INFO, "偏好设置默认值已初始化", null);
    }
} 